//加载wmts服务
const initWmtsMap = (url, layer, uuid) => {
  const wmts = new SuperMap3D.WebMapTileServiceImageryProvider({
    url: url,
    layer: layer,
    style: "default",
    format: "image/jpgpng",
    tileMatrixSetID: "1",
    tilingScheme: new SuperMap3D.GeographicTilingScheme(),
    tileMatrixLabels: [
      "1",
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "10",
      "11",
      "12",
      "13",
      "14",
      "15",
      "16",
      "17",
      "18",
      "19",
      "20",
    ],
  })
  const wmtsLayer = new SuperMap3D.ImageryLayer(wmts)
  for (let index = 0; index < window.viewer.imageryLayers.length; index++) {
    if (window.viewer.imageryLayers.get(index).uuid == uuid) {
      removeLayer(uuid)
    }
  }
  window.viewer.imageryLayers.add(wmtsLayer)
  wmtsLayer.uuid = uuid
  wmtsLayer.tname = '影像图层'
  return wmtsLayer;
};

// 添加地形地图服务
const initTerrainMap = (url) => {
  var provider = new SuperMap3D.TiandituTerrainProvider({
    token: 'cdbc867178d5ad16e8cf87982c5b3f2b',
    url: url,
    subdomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"]
  });
  window.viewer.terrainProvider = provider; // 设置地形
}

// 添加3d tiles 地图服务
const init3dTiles = async (url) => {
  const tileset = viewer.scene.primitives.add(await SuperMap3D.Cesium3DTileset.fromUrl(url, {
    skipLevelOfDetail: true,
    baseScreenSpaceError: 1024,
    skipScreenSpaceErrorFactor: 16,
    skipLevels: 1,
    immediatelyLoadDesiredLevelOfDetail: false,
    loadSiblings: false,
    cullWithChildrenBounds: true
  }));
  viewer.camera.viewBoundingSphere(tileset.boundingSphere, new SuperMap3D.HeadingPitchRange(0, -2, 0));
}

//加载天地图服务
const initTdtMap = (style) => {
  let mapStyle = null;
  switch (style) {
    case "TER_C":
      mapStyle = SuperMap3D.TiandituMapsStyle.TER_C;
      break;
    case "TER_W":
      mapStyle = SuperMap3D.TiandituMapsStyle.TER_W;
      break;
    case "VEC_C":
      mapStyle = SuperMap3D.TiandituMapsStyle.VEC_C;
      break;
  }
  window.viewer.imageryLayers.addImageryProvider(
    new SuperMap3D.TiandituImageryProvider({
      mapStyle: mapStyle, //天地图全球地形晕渲服务（经纬度投影）。
      token: "cdbc867178d5ad16e8cf87982c5b3f2b", //由天地图官网申请的密钥
    })
  );
};
const removeLayer = (uuid) => {
  for (let index = 0; index < window.viewer.imageryLayers.length; index++) {
    if (
      window.viewer.imageryLayers.get(index).uuid ==
      uuid
    ) {
      window.viewer.imageryLayers.remove(
        window.viewer.imageryLayers.get(index),
        true
      );
    }
  };
}


export { initWmtsMap, initTdtMap, removeLayer, initTerrainMap, init3dTiles }